<?php
if (!defined('admin')) {
    exit();
}
if (power('alevel') != 3) {
    die('error');
}
checktoken();
$cid = intval($_POST['cid']);
$showindexcount = M()->fetchcount("SELECT count(id) FROM " . tableex('moudle') . " where cid='$cid' and ifshow<>'0';");
$neworder = M()->fetchcount("SELECT morder FROM " . tableex('moudle') . " where cid='$cid' order by morder desc limit 1");
if (!$neworder) {
    $neworder = 0;
}
$ifshowtemp = '';
$msg = '';
if (isset($_POST['minfo'])) {
    M()->begin();
    foreach ($_POST['minfo'] as $key => $val) {
        $msetting = dbstr(json_encode(array('filterhtml' => 1)));//默认的html代码过滤规则 0不过滤 1过滤危险代码 2禁用代码 对超级管理员无效
        if (empty($_POST['minfo'][$key]) && !empty($_POST['mname'][$key])) {
            $_POST['minfo'][$key] = $_POST['mname'][$key];
        }
        if (strlen($_POST['minfo'][$key]) > 0) {
            $mname = dbstr(strtolower($_POST['mname'][$key]));
            $minfo = dbstr($_POST['minfo'][$key]);
            $mkind = intval($_POST['mkind'][$key]);
            if ($_POST['morder'][$key] == '') {
                $morder = $neworder + 5;
                $neworder = $morder;
            } else {
                $morder = intval($_POST['morder'][$key]);
            }
            if ($showindexcount == 0) {
                $ifshow = 1;
                $showindexcount = 1;
            } else {
                $ifshow = 0;
            }
            if ($mname == 'title') {
                $ifshow = 1;
                $showindexcount = 1;
                $msetting = dbstr(json_encode(array('filterhtml' => 1, 'adminsearch' => 1)));
            }
            if (strlen($mname) == 0 && strlen($minfo) > 1) {
                $mname = pinyin_permalink(str_cut(trim($minfo), 3));
            }
            $mname = trim($mname);
            if (strlen($mname) < 1) {
                $msg .= $minfo . ':字段名太短 ';
            } elseif (strlen($mname) > 20) {
                $msg .= $minfo . ':字段名太长 ';
            } elseif (!preg_match("/^[a-z][a-z0-9_]+$/", $mname)) {
                $msg .= $minfo . ':字段名只能为字母或字母+数字 ';
            } else {
                if (in_array($mname, $notallowfield)) {
                    $msg .= $minfo . ':字段名冲突,请不要使用保留的字段名 ';
                } else {
                    $pagecount = M()->fetchcount("SELECT count(id) FROM " . tableex('moudle') . " where mname='$mname' and cid='$cid'");
                    if ($pagecount > 0) {
                        $msg .= $minfo . ':已存在该字段 ';
                    } else {
                        $strdefault = '';
                        $thismkindinfo = inputkindinfo($mkind);
                        if (isset($thismkindinfo['kind']) && $thismkindinfo['kind'] == 'int') {
                            $strdefault = 0;
                        } else {
                            $strdefault = '';
                        }
                        if ($mkind == 9) {
                            $strdefault = 'now';
                        }
                        if ($mkind == 7) {
                            $strdefault = 1;
                        }
                        $query = M()->query("INSERT INTO `" . tableex('moudle') . "` (`mname` ,`minfo`,`mkind`,`morder`,`cid`,`ifshow`,`ifonly`,`strdefault`,`mcontent`,`ifshowtemp`,`hide`,`ifadmin`,`fid`,`msetting`,`ifcreated`) VALUES ('$mname','$minfo','$mkind','$morder','$cid','$ifshow','0','$strdefault','','$ifshowtemp','0','0','','$msetting','0');");
                        if (!$query) {
                            $msg .= $minfo . ':添加失败 ';
                        }
                    }
                }
            }
        }
    }
    M()->commit();
}
if (isset($_POST['createdtable']) && $_POST['createdtable'] == 1) {
    $query = M()->query("SELECT * FROM " . tableex('channel') . " where cid='$cid'");
    $link = M()->fetchOne();
    if (!isset($link['csetting'])) {
        $msg .= '请先进行栏目配置';
    } else {
        $csetting = json_decode($link['csetting'], 1);
        if (!isset($csetting['articletable']) || $csetting['articletable'] == tableex() || $csetting['articletable'] == '') {
            $msg .= '数据库表名尚未填写';
        }
        $articletable = $csetting['articletable'];
        M()->disconnect();
        $createreturn = channeltable($cid, $articletable);
        if ($createreturn === true) {
            $query = M()->query("UPDATE " . tableex('moudle') . " SET ifcreated='1' WHERE cid='$cid'");
        } elseif ($createreturn == 'no') {
        } else {
            $msg .= '数据库中创建字段失败';
        }
    }
}
if (!isset($_SERVER["HTTP_REFERER"])) {
    $_SERVER["HTTP_REFERER"] = '';
}
$url = $_SERVER["HTTP_REFERER"];
if ($msg == '') {
    adminmsg($url, '添加成功', 3);
} else {
    adminmsg($url, $msg, 10);
}

